package com.hjx.controller;

import com.hjx.entity.SysRole;
import com.hjx.entity.SysUser;
import com.hjx.logging.LogAnnotation;
import com.hjx.response.AjaxResponse;
import com.hjx.response.ResponseType;
import com.hjx.service.SysUserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2020/6/10 - 15:41
 */
@RestController
@RequestMapping("user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @LogAnnotation
    @PostMapping("/insert")
    public AjaxResponse insert(@RequestBody SysUser sysUser) {
        boolean result = sysUserService.insert(sysUser);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);

    }

    @LogAnnotation
    @GetMapping("/findOne/{uno}")
    public AjaxResponse findOne(@PathVariable String uno) {
        SysUser user = sysUserService.findOne(uno);
        if (user != null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, user);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    /**
     * 用户查看个人信息
     *
     * @return 查询的响应
     */
    @LogAnnotation
    @GetMapping("/findMyself")
    public AjaxResponse findMyself() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SysUser user = sysUserService.findUserAndRole(authentication.getName());
        if (user != null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, user);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    /**
     * 用户修改个人信息
     *
     * @param sysUser 修改的信息
     * @return 操作的响应
     */
    @LogAnnotation
    @PostMapping("/updateMyself")
    public AjaxResponse updateMyself(@RequestBody SysUser sysUser) {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (sysUser.getPassword() == null) {
            final boolean b = sysUserService.updateMyself(sysUser);
            if (b) {
                return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
            }
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @PostMapping("/updatePassword")
    public AjaxResponse updatePassword(@RequestBody Map<String, String> map) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (map.get("oldPassword") != null && map.get("newPassword") != null) {
            String password = sysUserService.findPassword(authentication.getName());
            if (passwordEncoder.matches(map.get("oldPassword"), password)) {
                String encode = passwordEncoder.encode(map.get("newPassword"));
                boolean b = sysUserService.updateMyself(new SysUser()
                        .setPassword(encode)
                        .setUno(authentication.getName()));
                if (b) {
                    return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
                }
            }
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @GetMapping("findAll")
    public AjaxResponse findAll() {
        List<SysUser> users = sysUserService.findAll();
        if (users != null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, users);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    @LogAnnotation
    @PostMapping("findBy")
    public AjaxResponse findBy(@RequestBody SysUser sysUser) {
        List<SysUser> users = sysUserService.findBy(sysUser);
        if (users != null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, users);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }


    @LogAnnotation
    @PostMapping("update")
    public AjaxResponse update(@RequestBody SysUser sysUser) {
        final boolean b = sysUserService.update(sysUser);
        if (b) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @GetMapping("delete/{uid}")
    public AjaxResponse delete(@PathVariable String uid) {
        final boolean b = sysUserService.delete(uid);
        if (b) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @GetMapping("findUserAndRole/{uno}")
    public AjaxResponse findUserAndRole(@PathVariable String uno){
        SysUser user = sysUserService.findUserAndRole(uno);
        if (user!=null){
            return new AjaxResponse(ResponseType.QUERY_SUCCESS,user);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    @LogAnnotation
    @GetMapping("findAllUserAndRole")
    public AjaxResponse findAllUserAndRole(){
        List<SysUser> list = sysUserService.findAllUserAndRole();
        if (list!=null){
            return new AjaxResponse(ResponseType.QUERY_SUCCESS,list);
        }
        return new AjaxResponse(1,"未查询到数据",null);
    }



}
